//
//  QuDanSearchController.m
//  QuDanOEM
//
//  Created by mac on 2021/10/20.
//

#import "QuDanSearchController.h"
#import "QuDanSearchContentViewCell.h"
#import "QuDanSearchModel.h"
#import "QuDanSearchTextfiled.h"
#import "QuDanUserCetnerMainController.h"
#import "QuDanSearchNetWork.h"
#import "QuDanSearchHeaderViewCell.h"
@interface QuDanSearchController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *searchCollectionView;
@property (nonatomic,strong)NSMutableArray *searchListArr;
@property (nonatomic,strong)NSMutableArray  *recommendListArr;
@property (nonatomic, strong) QuDanSearchTextfiled *searchTextField;
@property (nonatomic,strong)UIButton *cancelBtn;
@end
@implementation QuDanSearchController
- (NSMutableArray *)recommendListArr
{
    if (!_recommendListArr) {
        _recommendListArr = [NSMutableArray array];
    }
    return _recommendListArr;
}
- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setBounds:CGRectMake(0, 0, 50, 30)];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(actionSearchCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
-(void)actionSearchCancelBtn:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (QuDanSearchTextfiled *)searchTextField{
    if(!_searchTextField){
        _searchTextField = [[QuDanSearchTextfiled alloc]initWithFrame:CGRectMake(0, 0, QD_IPHONE_WIDTH - 80, 32)];
        _searchTextField.font = [UIFont systemFontOfSize:15];
        _searchTextField.textColor = [UIColor blackColor];
        _searchTextField.layer.cornerRadius = 16;
        _searchTextField.clipsToBounds = YES;
        _searchTextField.backgroundColor = QD_RGB(245, 245, 245);
        _searchTextField.logoColor = HEXCOLOR(0x999999);
        NSMutableAttributedString *attribuedString = [[NSMutableAttributedString alloc]initWithString:@"搜索用户id昵称"];
        [attribuedString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:HEXCOLOR(0x999999)} range:NSMakeRange(0, attribuedString.length)];
        _searchTextField.attributedPlaceholder = attribuedString;
    }
    return _searchTextField;
}

- (NSMutableArray *)searchListArr
{
    if (!_searchListArr) {
        _searchListArr = [NSMutableArray array];
    }
    return _searchListArr;
}
- (UICollectionView *)searchCollectionView
{
    if (!_searchCollectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.sectionHeadersPinToVisibleBounds = NO;//头视图悬浮
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        flow.minimumLineSpacing = 10;//行间距
        flow.minimumInteritemSpacing = 10;//列间距
        _searchCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
        _searchCollectionView.backgroundColor =  [UIColor clearColor];
        _searchCollectionView.showsVerticalScrollIndicator = NO;
        _searchCollectionView.showsHorizontalScrollIndicator = NO;
        _searchCollectionView.delegate = self;
        _searchCollectionView.dataSource = self;
        [_searchCollectionView registerNib:[UINib nibWithNibName:@"QuDanSearchContentViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanSearchContentViewCell"];
        [_searchCollectionView registerNib:[UINib nibWithNibName:@"QuDanSearchHeaderViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanSearchHeaderViewCell"];
    }
    return _searchCollectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = QD_RGB(247, 248, 252);
    self.navigationItem.titleView = self.searchTextField;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.cancelBtn];
    WEAKSELF
    [self.searchTextField setSearchBlock:^(NSString * _Nonnull keyword) {
        NSLog(@"keywordkeyword:%@",keyword);
        [weakSelf setupLoadSearchWithKeyWord:keyword];
    }];
}
-(void)setupLoadSearchWithKeyWord:(NSString *)keyword
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"kw"] = keyword;
    [[QuDanSearchNetWork shareInstance]requestSearhcUserTNetWork:param viewController:self andBlock:^(id  _Nullable data, BOOL result) {
        NSLog(@"搜索数据data.%@",data);
        [self.searchListArr removeAllObjects];
        [self.recommendListArr removeAllObjects];
        self.searchListArr = [QuDanSearchModel mj_objectArrayWithKeyValuesArray:data[@"result"]];
        self.recommendListArr = [QuDanSearchModel mj_objectArrayWithKeyValuesArray:data[@"recommend"]];
        [self setupSearchUI];
        [self.searchCollectionView reloadData];
    }];
}
-(void)setupSearchUI
{
    [self.view addSubview:self.searchCollectionView];
    [self.searchCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
}
#pragma mark -- QuDanSearchController 代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.searchListArr.count;
    }else{
        return self.recommendListArr.count+1;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        QuDanSearchContentViewCell *searchCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSearchContentViewCell" forIndexPath:indexPath];
        searchCell.searchModel = self.searchListArr[indexPath.row];
        return searchCell;
    }else{
        if (indexPath.row == 0) {
            QuDanSearchHeaderViewCell *headerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSearchHeaderViewCell" forIndexPath:indexPath];
            return headerCell;
        }else{
            QuDanSearchContentViewCell *searchCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSearchContentViewCell" forIndexPath:indexPath];
            searchCell.searchModel = self.recommendListArr[indexPath.row-1];
            return searchCell;
        }
    }
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(QD_IPHONE_WIDTH-20 ,65);
    }else{
        if (indexPath.row == 0) {
            return CGSizeMake(QD_IPHONE_WIDTH-20 ,35);
        }else{
            return CGSizeMake(QD_IPHONE_WIDTH-20 ,65);
        }
    }
 
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    QuDanUserCetnerMainController *userCenterDetailvc = [[QuDanUserCetnerMainController alloc]init];
    userCenterDetailvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:userCenterDetailvc animated:YES];
}
@end
